package flex.cc.basicinformation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import flex.cc.PublicUtil;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.basicinformation.service.ValueAddedServiceProviderConfigService;
import flex.cc.basicinformation.service.ValueAddedServiceProviderService;
import flex.cc.basicinformation.vo.ConfigVo;
import flex.cc.basicinformation.vo.ValueAddedServiceProviderVo;
import flex.cc.core.annotation.LogAnnotation;
import flex.cc.core.annotation.ValidateAnnotation;
import flex.cc.core.support.BaseController;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wkangle
 * @className ValueAddedServiceProviderController
 * @description 增值服务服务商【2.2-服务包配置需求】
 * @time 2022-02-23 14:46:00
 */
@RestController
@RequestMapping(value = "/basic/information/valueadded/serviceprovider")
public class ValueAddedServiceProviderController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ValueAddedServiceProviderController.class);

    @Autowired
    private ValueAddedServiceProviderService valueAddedServiceProviderService;
    @Autowired
    private ValueAddedServiceProviderConfigService valueAddedServiceProviderConfigService;


    @PostMapping(value = "/queryListWithPage")
    @ResponseBody
    @ApiOperation(httpMethod = "POST", value = "查询增值服务服务商")
    public Wrapper<IPage<ValueAddedServiceProviderVo>> queryListWithPage(@ApiParam(name = "reqVo", value = "请求信息") @RequestBody Map<String, Object> reqVo) {
        logger.info("{} /valueadded/serviceprovider/queryListWithPage ==> {}", new Date(), reqVo);
        ValueAddedServiceProviderVo vo = new ValueAddedServiceProviderVo();
        vo.setServiceName(PublicUtil.isNotEmpty(reqVo.get("serviceName")) ? reqVo.get("serviceName").toString() : null); // 服务项目名称，支持模糊查询
        vo.setServiceType(PublicUtil.isNotEmpty(reqVo.get("serviceType")) ? reqVo.get("serviceType").toString() : null); // 服务类型
        vo.setStatus(PublicUtil.isNotEmpty(reqVo.get("status")) ? reqVo.get("status").toString() : null); // 状态
        vo.setPageNum(Integer.parseInt(String.valueOf(reqVo.get("pageNum"))));
        vo.setPageSize(Integer.parseInt(String.valueOf(reqVo.get("pageSize"))));
        IPage<ValueAddedServiceProviderVo> page = valueAddedServiceProviderService.queryListWithPage(vo);
        return WrapMapper.ok(page);
    }

    /**
     * @param id 增值服务商主键
     * @param status 目标状态
     * @return
     * @description 启用增值服务商【2.2-服务包配置需求】
     * @author wkangle
     * @time 2022/02/23 0023 18:08
     * @exception/throws
     */
    @LogAnnotation
    @GetMapping(value = "/switchStatus/{id}/{status}")
    @ApiOperation(httpMethod = "GEt", value = "增值服务商状态修改")
    public Wrapper<Map<String, String>> switchStatus(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable(value = "id") Long id, @PathVariable(value = "status") String status) {
        logger.info("{} /valueadded/serviceprovider/enableValueAddedServiceProvider ==> {}, {}", new Date(), id, status);
        Map<String, String> switchStatus = valueAddedServiceProviderService.switchStatus(id, status);
        return WrapMapper.ok(switchStatus);
    }

    /**
     * @param vo 增值服务商相关信息
     * @return
     * @description 新增/编辑增值服务商【2.2-服务包配置需求】
     * @author wkangle
     * @time 2022/02/23 0023 18:08
     * @exception/throws
     */
    @PostMapping(value = "/saveValueAddedServiceProvider")
    @ApiOperation(httpMethod = "POST", value = "新增/编辑增值服务商信息")
    @ValidateAnnotation
    @LogAnnotation
    public Wrapper<Map<String, String>> saveValueAddedServiceProvider(@ApiParam(name = "action", value = "增值服务商信息") @RequestBody ValueAddedServiceProviderVo vo) {
        logger.info("{} /valueadded/serviceprovider/disableValueAddedServiceProvider ==> {}", new Date(), vo);
        LoginAuthDto loginAuthDto = this.getLoginAuthDto();
        Map<String, String> result = valueAddedServiceProviderService.saveValueAddedServiceProvider(loginAuthDto, vo);
        return WrapMapper.ok(result);
    }

    /**
     * @param id 增值服务商主键id
     * @return
     * @description 根据增值服务商主键id查询一个，用于修改页面回显
     * @author wkangle
     * @time 2022/02/28 0028 10:22
     * @exception/throws
     */
    @GetMapping(value = "/queryOne/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询一个用于修改")
    public Wrapper<ValueAddedServiceProviderVo> queryOne(@ApiParam(name = "action", value = "增值服务商id") @PathVariable(value = "id") Long id) {
        logger.info("{} /valueadded/serviceprovider/queryOne ==> {}", new Date(), id);
        ValueAddedServiceProviderVo vo = valueAddedServiceProviderService.queryOne(id);
        return WrapMapper.ok(vo);
    }

    @GetMapping(value = "/checkServiceName/{serviceName}/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询增值服务商名称是否重复")
    public Wrapper<String> checkServiceName(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable(value = "serviceName") String serviceName, @PathVariable(value = "id") String id) {
        String checkServiceName = valueAddedServiceProviderService.checkServiceName(serviceName, id);
        return WrapMapper.ok(checkServiceName);
    }

    @GetMapping(value = "/checkApiCode/{apiCode}/{id}")
    @ApiOperation(httpMethod = "GET", value = "查询增值服务商标识是否重复")
    public Wrapper<String> checkApiCode(@ApiParam(name = "reqVo", value = "请求信息") @PathVariable(value = "apiCode") String apiCode, @PathVariable(value = "id") String id) {
        String checkApiCode = valueAddedServiceProviderService.checkApiCode(apiCode, id);
        return WrapMapper.ok(checkApiCode);
    }

    @GetMapping(value = "/initConfigTypeList")
    @ApiOperation(httpMethod = "GET", value = "初始化配置类型+接口类型/密钥类型")
    public Wrapper<List<ConfigVo>> initConfigTypeList() {
        List<ConfigVo> initConfigTypeList = valueAddedServiceProviderConfigService.initConfigTypeList();
        return WrapMapper.ok(initConfigTypeList);
    }

}
